package com.tianyang.yukuaifu.business.main.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.facebook.drawee.view.SimpleDraweeView;
import com.tianyang.yukuaifu.R;
import com.tianyang.yukuaifu.base.BaseTitleActivity;
import com.tianyang.yukuaifu.business.main.contract.InviteCodeContract;
import com.tianyang.yukuaifu.business.main.presenter.InviteCodePresenter;
import com.tianyang.yukuaifu.common.utils.CommonViewUtils;
import com.tianyang.yukuaifu.utils.DownloadImgUtils;
import com.tianyang.yukuaifu.utils.ToastUtil;
import com.tianyang.yukuaifu.utils.permissions.PermissionsConstant;
import com.tianyang.yukuaifu.utils.permissions.PermissionsMgr;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Description：邀请
 * Created by xyj on 2024/3/7
 */
public class InviteCodeActivity extends BaseTitleActivity implements InviteCodeContract.View {

    private String permission = PermissionsConstant.WRITE_STORAGE;

    @BindView(R.id.leftTitleTv)
    TextView leftTitleTv;
    @BindView(R.id.rightTitleTv)
    TextView rightTitleTv;
    @BindView(R.id.shareViewRl)
    FrameLayout shareViewRl;
    @BindView(R.id.qrCodeIv)
    SimpleDraweeView qrCodeIv;

    private InviteCodeContract.Presenter mPresenter;

    private boolean isShowAgent;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_invite_code;
    }

    @Override
    protected void initView() {
        super.initView();
        commonTitle.setTitle("邀请");
        mPresenter = new InviteCodePresenter(this);
        mPresenter.compositePoster(0);
    }

    @Override
    public void showCompositePoster(String url) {
        if (!TextUtils.isEmpty(url)) {
            qrCodeIv.setImageURI(Uri.parse(url));
        }
        shareViewRl.destroyDrawingCache();
    }

    @OnClick({R.id.leftTitleTv, R.id.rightTitleTv, R.id.downloadTv, R.id.shareTv})
    public void onClick(View view) {
        if (!isRespondEvent() || getContext() == null) return;
        switch (view.getId()) {
            case R.id.leftTitleTv:
                if (isShowAgent) {
                    isShowAgent = false;
                    leftTitleTv.setBackgroundResource(R.drawable.img_left_choose);
                    leftTitleTv.setTextColor(ContextCompat.getColor(mContext, R.color.white));
                    rightTitleTv.setBackgroundResource(R.drawable.img_right_normal);
                    rightTitleTv.setTextColor(ContextCompat.getColor(mContext, R.color.black));
                    shareViewRl.setBackgroundResource(R.drawable.img_share_merchant);
                    mPresenter.compositePoster(0);
                }
                break;
            case R.id.rightTitleTv:
                if (!isShowAgent) {
                    isShowAgent = true;
                    leftTitleTv.setBackgroundResource(R.drawable.img_left_normal);
                    leftTitleTv.setTextColor(ContextCompat.getColor(mContext, R.color.black));
                    rightTitleTv.setBackgroundResource(R.drawable.img_right_choose);
                    rightTitleTv.setTextColor(ContextCompat.getColor(mContext, R.color.white));
                    shareViewRl.setBackgroundResource(R.drawable.img_share_agent);
                    mPresenter.compositePoster(1);
                }
                break;
            case R.id.downloadTv:
                if (!PermissionsMgr.getInstance().hasPermission(mContext, permission)) {
                    requestPermissions(permission);
                    return;
                }
                downloadClick();
                break;
            case R.id.shareTv:
                shareClick();
                break;
        }
    }

    private void shareClick() {
        shareViewRl.postDelayed(new Runnable() {
            @Override
            public void run() {
                shareViewRl.setDrawingCacheEnabled(true);
                Bitmap mClipScreen = shareViewRl.getDrawingCache();
                if (mClipScreen == null || mClipScreen.isRecycled()) return;
                CommonViewUtils.shareToWeChat(mContext, mClipScreen);
            }
        }, 500);
    }

    private void downloadClick() {
        shareViewRl.postDelayed(new Runnable() {
            @Override
            public void run() {
                shareViewRl.setDrawingCacheEnabled(true);
                Bitmap mClipScreen = shareViewRl.getDrawingCache();
                if (mClipScreen == null || mClipScreen.isRecycled()) return;
                boolean isSuccess = DownloadImgUtils.saveBitmap(mContext, mClipScreen);
                if (isSuccess) {
                    ToastUtil.show("二维码保存成功！");
                } else {
                    ToastUtil.show("二维码保存失败, 请重试！");
                }
            }
        }, 500);
    }

    @Override
    public void onPermissionGranted(List<String> permissions) {
        if (permissions != null && permissions.contains(permission)) {
            downloadClick();
        }
    }

    @Override
    protected boolean hasBindEventBus() {
        return false;
    }

    @Override
    public void setPresenter(InviteCodeContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
     }
}
